package com.future.network

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkRequest
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.LifecycleObserver
import com.future.network.info.NetworkController
import com.future.network.info.NetworkInterface2
import com.future.network.receiver.NetworkCallbackImpl
import com.future.network.utiles.DebugLogs
import kotlin.collections.ArrayList


class NetworkManager : NetworkInterface2 {
    private val networkController = ArrayList<NetworkController>()
    private var networkCallbackImpl: NetworkCallbackImpl? = null
    private var isInit = false
    private lateinit var context: Context

    companion object {

        private var instance: NetworkManager? = null
            get() {
                if (field == null) {
                    field = NetworkManager()
                }
                return field
            }

        @Synchronized
        fun get(): NetworkManager {
            return instance!!
        }
    }

    init {
        networkCallbackImpl = NetworkCallbackImpl()
    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun getNetworkCallBackImpl(): NetworkCallbackImpl {
        if (networkCallbackImpl == null) {
            networkCallbackImpl = NetworkCallbackImpl()
        }
        return networkCallbackImpl as NetworkCallbackImpl
    }

    /**
     * 初始化方法，初始网络监听，根据不同版本监听不同的处理
     */
    fun init(context: Context) {
        if (isInit) {
            DebugLogs.show("进行了多次初始化!")
            return
        }
        this.context = context.applicationContext
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {//版本大于26时
                connectivityManager.registerDefaultNetworkCallback(getNetworkCallBackImpl())
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {//API 大于21时
                val builder = NetworkRequest.Builder()
                val request = builder.build()
                connectivityManager.registerNetworkCallback(request, getNetworkCallBackImpl())
            }
        }
        isInit = true
    }

    /**
     * 注册
     */
    override fun registerObserver(observer: NetworkController) {
        if (!networkController.contains(observer)) {
            networkController.add(observer)
        }
    }

    /**
     * 注销
     */
    override fun unRegisterObserver(observer: NetworkController) {
        if (networkController.isNotEmpty()) {
            networkController.remove(observer)
        }
    }

    override fun unRegisterAllObserver() {
        if (networkController.isNotEmpty()) {
            networkController.clear()
        }
    }

    /**
     * 发送当前网络状态
     */
    override fun post(networkType: NetworkType) {
        networkController.forEach {
            it.networkType(networkType)
        }
    }
}